package top.yxqz.springmvc.annotation.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @author: 余小小
 * @date: 2023/5/18
 * @description：初始化配置信息 顶层接口是：WebApplicationInitializer
 */
public class InitWeb extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * spring的配置文件
     *
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    /**
     * springmvc的配置文件
     *
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMVCConfig.class};
    }

    /**
     * 配置前端控制器可以操作的映射路径（servlet）
     *
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        String mapping[] = new String[]{"/"};
        return mapping;
    }

    /**
     * 配置过滤器
     *
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
//    创建过滤器
//        1.字符编码过滤器
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("UTF-8");
        encodingFilter.setForceEncoding(true);
//     2.特殊请求方式过滤器
        HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
        Filter myFilters[] = new Filter[]{encodingFilter, methodFilter};
        return myFilters;
    }
}
